<?php
/**
 * Dillenm-Mvc-Framework
 * 
 * A PHP MVC Framework written by Dillen Meijboom, all rights belong to him.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.library.Form.Validator
 * @url http://code.google.com/p/dillenm-mvc-framework/
 */

/**
 * Check if the mail is valid (through preg_match), if the value param is set to true it will also check DNS records.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.library.Form.Validator
 */

class Dmf_Form_Validator_Email extends Dmf_Form_Validator
{
    protected $_message = 'Please fill in a valid e-mail address';
    
    public function isValid( $value )
    {
        if ( ! preg_match( '/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $value ) )
        {
            return false;
        }
        
        if ( ! $this->_value )
        {
            return true;
        }
        else
        {
            $parts = explode( '@', $value );
            
            return checkdnsrr( $parts['1'], 'MX' );
        }
    }
}